package com.pr.makfilm.user.pages.room.details.cmp;

import com.pr.makfilm.core.cmp.notify.Notification;
import com.pr.makfilm.core.cmp.web.BootstrapDatePicker;
import com.pr.makfilm.core.cmp.web.BootstrapTextArea;
import com.pr.makfilm.core.cmp.web.BootstrapTextFieldPanel;
import com.pr.makfilm.core.cmp.web.BootstrapTimePicker;
import com.pr.makfilm.core.cmp.window.AbstractWindow;
import com.pr.makfilm.domain.Event;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.service.IEventService;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;

/**
 * Created with IntelliJ IDEA.
 * User: Barbara
 * Date: 21.10.13
 * Time: 23:51
 * To change this template use File | Settings | File Templates.
 */
public class RoomEventWindow extends AbstractWindow<Room>
{
   @SpringBean
   private IEventService eventService;

   private Event event;

   private String date;

   private String time;

   public RoomEventWindow(String id, IModel<Room> model)
   {
      super(id, model, new ResourceModel("new.event"));
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      BootstrapTextFieldPanel nameField = new BootstrapTextFieldPanel<String>("name",
              new PropertyModel<String>(this, "event." + Event.FIELD_NAME));
      nameField.setValidation(true);
      form.add(nameField);
      form.add(new BootstrapTextArea<String>("description",
              new PropertyModel<String>(this, "event." + Event.FIELD_DESCRIPTION)));
      PriceField priceField = new PriceField("price",
              new PropertyModel<Double>(this, "event." + Event.FIELD_PRICE));
      priceField.setValidation(true);
      form.add(priceField);
      BootstrapDatePicker dateField = new BootstrapDatePicker("date",
              new PropertyModel<String>(this, "date"));
      dateField.setValidation(true);
      form.add(dateField);
      BootstrapTimePicker timeField = new BootstrapTimePicker("time",
              new PropertyModel<String>(this, "time"));
      timeField.setValidation(true);
      form.add(timeField);
   }

   @Override
   public void onSubmit(AjaxRequestTarget target, Form<?> form)
   {
      Room room = (Room) getDefaultModelObject();
      event.setRoom(room);
      String[] dateArray = StringUtils.split(date, '-');
      String[] timeArray = StringUtils.split(time, ':');
      DateTime dateTime = new DateTime();

      dateTime = dateTime.withDayOfMonth(Integer.parseInt(dateArray[0]));
      dateTime = dateTime.withMonthOfYear(Integer.parseInt(dateArray[1]));
      dateTime = dateTime.withYear(Integer.parseInt(dateArray[2]));

      dateTime = dateTime.withHourOfDay(Integer.parseInt(timeArray[0]));
      dateTime = dateTime.withMinuteOfHour(Integer.parseInt(timeArray[1]));
      dateTime = dateTime.withSecondOfMinute(0);
      dateTime = dateTime.withMillisOfSecond(0);
      event.setDateTime(dateTime);
      event = eventService.save(event);
      Notification.success(getString("event.successfully.created"));
   }

   @Override
   protected void resetState()
   {
      event = new Event();
   }
}
